package com.gxa.insurance.controller;

import com.gxa.insurance.pojo.RepairDepot;
import com.gxa.insurance.service.RepairDepotService;
import com.gxa.insurance.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wang li sha
 * @date 2022-06-06 17:58
 */
@Api(value = "修理厂repairDepot相关的api接口")
@RestController
@RequestMapping("/repairDepot")
@CrossOrigin
public class RepairDepotController {
    @Autowired
    RepairDepotService repairDepotService;

    @ApiOperation("添加修理厂的方法")
    @PostMapping("/insert")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", paramType = "header")
    })
    public Map insert(@RequestBody RepairDepot repairDepot, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Integer insert = repairDepotService.insert(repairDepot);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(insert);
        return jsonResult.getMap();
    }

    @ApiOperation("修改修理厂信息的方法")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", paramType = "header")
    })
    public Map update(@RequestBody RepairDepot repairDepot, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Integer update = repairDepotService.update(repairDepot);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }

    @ApiOperation("根据id删除修理厂的方法")
    @PostMapping("/deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", paramType = "header")
    })
    public Map deleteById(@RequestParam("id") Integer id, @RequestHeader(value = "token", required = false) String token) throws Exception {
        Integer delete = repairDepotService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(delete);
        return jsonResult.getMap();
    }

    @ApiOperation("根据id查询修理厂的方法")
    @PostMapping("/selectById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", paramType = "header")
    })
    public Map selectById(@RequestParam("id") Integer id, @RequestHeader(value = "token", required = false) String token) throws Exception {
        RepairDepot repairDepot = repairDepotService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(repairDepot);
        return jsonResult.getMap();
    }

    @ApiOperation("分页查询所有修理厂的方法")
    @PostMapping("/selectAllByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页记录数", required = true, dataType = "int", paramType = "query", defaultValue = "5"),
            @ApiImplicitParam(name = "token", value = "用户token", paramType = "header")
    })
    public Map selectAllByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestHeader(value = "token", required = false) String token) throws Exception {
        List<RepairDepot> repairDepots = repairDepotService.selectAllByPage(page, size);
        Integer count = repairDepotService.selectAll().size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(repairDepots);
        jsonResult.put("count", count);
        return jsonResult.getMap();
    }

    @ApiOperation("分页模糊查询所有修理厂的方法")
    @PostMapping("/selectLikeNameByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页记录数", required = true, dataType = "int", paramType = "query", defaultValue = "5"),
            @ApiImplicitParam(name = "name", value = "修理厂名", required = true, dataType = "string", paramType = "query", defaultValue = "成都"),
            @ApiImplicitParam(name = "token", value = "用户token", paramType = "header")
    })
    public Map selectLikeNameByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("name") String name, @RequestHeader(value = "token", required = false) String token) throws Exception {
        List<RepairDepot> repairDepots = repairDepotService.selectLikeNameByPage(page, size, name);
        Integer count = repairDepotService.selectLikeName(name).size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(repairDepots);
        jsonResult.put("count", count);
        return jsonResult.getMap();
    }
}
